package com.hy.dao;

import com.hy.bean.Card;
import com.hy.bean.User;
import com.hy.utils.DBManager;
import com.hy.utils.DateUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class LoginDao {
    private QueryRunner qr = new QueryRunner();

    public List<User> selectLogin(String userName, String password) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select * from ums_user where phone  = ? and password = ?  and tombstone = '0'";
        BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
        List<User> query = qr.query(conn, sql, new BeanListHandler<>(User.class, brp), userName, password);
        DbUtils.close(conn);
        return query;
    }

    public boolean selectPhone(String phone) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select * from ums_user where phone = ? and tombstone = '0'";
        User query = qr.query(conn, sql, new BeanHandler<>(User.class), phone);
        DbUtils.close(conn);
        return query != null ? true : false;
    }

    public boolean insertUser(User user) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "insert into ums_user(`user_name`, `password`, `phone`, `create_time`) values(?,?,?,?)";
        int update = qr.update(conn, sql, user.getUserName(), user.getPassword(), user.getPhone(), DateUtil.datetime());
        DbUtils.close(conn);
        return update > 0 ? true : false;
    }


}
